<template>
  <div>
    <br />
    <br />
    <router-link
      to="/systemManage/user"
      style="margin-left: 40px;margin-top: 20px;color:blue;"
    >返回用户管理</router-link>
    <h2 style="text-align: center;">用户菜单编辑</h2>
    <el-row :gutter="20" style="margin-left: 40px;">
      <el-col :span="5">当前编辑账号：{{editInfo.username}}</el-col>
      <el-col :span="5">当前编辑用户名：{{editInfo.name}}</el-col>
      <el-col :span="5">归属机构：{{editInfo.comName}}</el-col>
      <el-col :span="5">所属部门：{{editInfo.department}}</el-col>
    </el-row>
    <el-table :data="tableData" style="width: 85%;margin: 30px 40px;">
      <el-table-column prop="id" label="ID" width="45"></el-table-column>
      <el-table-column prop="menuName" label="菜单名称" width="150"></el-table-column>
      <el-table-column prop="parentId" label="节点" width="80"></el-table-column>
      <el-table-column prop="path" label="路径" width="350"></el-table-column>
      <el-table-column prop="status" label="权限状态" width="80"></el-table-column>
      <el-table-column fixed="right" label="操作" width="125">
        <template slot-scope="scope">
          <el-button type="text" @click="handleClick(scope.row)" size="small">添加或移除权限</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-button type="primary" @click="save" style="margin: 20px 40%;">保存提交</el-button>
  </div>
</template>

<script>
import { updateUserMenu, getMenus } from "@/api/user";
export default {
  name: "editMenu",
  data() {
    return {
      editInfo: this.$route.query.editInfo,
      tableData: []
    };
  },
  mounted() {
    this.getMenus();
  },
  methods: {
    getMenus() {
      // 初始化数据表
      getMenus(this.editInfo.systemCode, this.editInfo.username).then(res => {
        let list = res.data;
        for (let i = 0; i < list.length; i++) {
          if (list[i].status === 1) list[i].status = "有";
          else list[i].status = "无";
          if (list[i].parentId === 0) list[i].parentId = "父节点";
          else list[i].parentId = "字节点";
        }
        this.tableData = list;
      });
    },
    save() {
      const data = this.tableData;
      let menuId = [];
      let status = [];
      let parentId = [];
      for (let i = 0; i < data.length; i++) {
        menuId.push(data[i].id);
        if (data[i].status === "有") {
          status.push(1);
        } else {
          status.push(0);
          if (data[i].parentId === "父节点") {
            parentId.push(data[i].id);
          }
        }
      }
      updateUserMenu({
        systemCode: this.editInfo.systemCode,
        username: this.editInfo.username,
        menuIds: menuId.toString(),
        status: status.toString(),
        parentIds: parentId.toString()
      }).then(res => {
        this.$message({
          message: res.message,
          type: "success"
        });
        this.getMenus();
      });
    },
    handleClick(row) {
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.tableData[i].id === row.id) {
          if (this.tableData[i].status === "有") {
            this.tableData[i].status = "无";
            this.$message({
              message: "已移除 " + row.menuName + " 菜单权限",
              type: "warning"
            });
          } else {
            this.tableData[i].status = "有";
            this.$message({
              message: "已添加 " + row.menuName + " 菜单权限",
              type: "success"
            });
          }
          break;
        }
      }
    }
  }
};
</script>

<style>
</style>